package com.stenio.fancat.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.stenio.fancat.exception.FanCatException;
import com.stenio.fancat.form.LoginForm;
import com.stenio.fancat.service.LoginService;
import com.stenio.fancat.util.ResultUtil;
import com.stenio.fancat.vo.LoginVO;
import com.stenio.fancat.vo.Result;

/**
 * 登录模块controller
 * @author stenio
 */
@RestController
public class LoginController {

	@Autowired
	private LoginService loginService;

//	@RequestMapping("getSmsCode")
	@PostMapping("getSmsCode")
	@ResponseBody
	public Result getVerifyCode(@RequestBody LoginForm form) {
		try {
			if(null == form || StringUtils.isBlank(form.getPhone())){
				return ResultUtil.error("1", "手机号不能为空");
			}
			loginService.getSmsCode(form.getPhone());
		} catch (FanCatException e) {
			return ResultUtil.error("1", "发送信息失败");
		}
		return ResultUtil.success(null);
	}

//	@RequestMapping("login")
	@PostMapping("login")
	@ResponseBody
	public Result login(@RequestBody LoginForm form) {
		try {
			LoginVO vo = loginService.login(form);
			return Result.success(vo);
		} catch (FanCatException e) {
			return ResultUtil.error("1", e.getMessage());
		}
	}

	/**
	 * 等三方登录
	 * @param form 参数实体
	 * @return
	 */
	@PostMapping("thirdLogin")
	@ResponseBody
	public Result thirdLogin(@RequestBody LoginForm form) {
		try {
			LoginVO vo = loginService.thirdLogin(form);
			return Result.success(vo);
		} catch (Exception e) {
			return ResultUtil.error("1", e.getMessage());
		}
	}

}
